package com.zntg.schedule.appToCrm;

import com.zntg.bo.CrmAcceDtlBo;
import com.zntg.bo.TblobstorageBo;
import com.zntg.mapper.aiiidb.CrmAcceDtlMapper;
import com.zntg.mapper.crmdb.TblobtSorageMapper;
import com.zntg.utils.ComponentFactory;
import com.zntg.utils.DownloadPdf;
import com.zntg.utils.string.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ScheduledTaskAppToCrm {
    @Value("${template.targetPath}")
    String targetPath;
    @Value("${template.renamePath}")
    String renamePath;
    @Value("${spring.profiles.active}")
    String active;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTaskAppToCrm.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

   //@Scheduled(cron = "0 0 1 * * ?")   //每天凌晨1点执行
    @Scheduled(cron = "0 0/1 * * * ?")   //每1分钟
   /*@Scheduled(fixedRate = 50000000)*/
    public void appToCrm() {
       // System.out.println("定时从app文件服务器获取所有附件信息,并同步到crm文件服务器"+active);
            int newCount=0;
            int oldCount=0;
            try {
                CrmAcceDtlMapper crmAcceDtlMapper = ComponentFactory.getBean(CrmAcceDtlMapper.class);
                TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
                Map<String,Object> queryMap=new HashMap<>();
                List<CrmAcceDtlBo> appFileList=crmAcceDtlMapper.findAllList(queryMap);//app所有附件信息
                //客服app所有附件信息
                int size=0;
                if(!StringUtil.isNullOrEmpty(appFileList)){
                    size=appFileList.size();
                }
                if(size>0){
                    //System.out.println("总条数========================"+size);
                    for(int i=0;i<size;i++){
                        CrmAcceDtlBo appFileBo=appFileList.get(i);
                        TblobstorageBo crmFileBo=new TblobstorageBo();
                        String lStorageid=appFileBo.getlStorageid();
                        crmFileBo.setlStorageid(lStorageid);
                        String cCatalog=appFileBo.getcCatalog();
                        crmFileBo.setcCatalog(cCatalog);
                        String fileUrl=appFileBo.getFileUrl();
                        String dCreateDate=appFileBo.getdCreatedate();
                        //获取原始文件存放地址
                        // String oFileUrl=ipAddress+"/"+cFilePath.replaceAll("\\\\","/")+"baccessory";
                        String appFilename=appFileBo.getAppFilename();
                        String cSubKeyId=appFileBo.getcSubkeyid();
                        String fileName=lStorageid+"baccessory";
                        if(!StringUtil.isNullOrEmpty(dCreateDate)){
                            dCreateDate=dCreateDate.substring(0,10);
                        }
                        String localFilePath=targetPath+dCreateDate+"/Tblobstorage/"+lStorageid.substring(0,lStorageid.length()-1)+"/";
                        localFilePath=localFilePath.replaceAll("\\\\","/")+"/";
                        //判断文件是否已经存在
                        TblobstorageBo query=new TblobstorageBo();
                        query.setlStorageid(lStorageid);
                        // System.out.println("fileUrl=========================="+fileUrl);
                        DownloadPdf.downLoadServerFileToLocal(fileUrl,fileName,localFilePath);
                        String cCreator="";
                        //if(flag){ 有更新的情况，编辑的时候，所有不能加flag判断
                            int count=tblobtSorageMapper.count(query);
                            if(count==0) {
                                Map<String,Object> creatorMap=new HashMap<>();
                                creatorMap.put("lSerialNo",lStorageid);
                                creatorMap.put("lIdentityscan",lStorageid);
                                creatorMap.put("cCatalog",cCatalog);
                                //查询c_creator
                                if("TQUALIFYINVESTOR_REQ".equals(cCatalog) ||
                                        "TCUSTREGISTERREQ".equals(cCatalog)||
                                        "TINVESTOR_REQ".equals(cCatalog)||
                                        "TCAPITALCURRENTS".equals(cCatalog)||
                                        "TCUSTREGISTERREQ_MAIN".equals(cCatalog)||
                                        "TBENEFICIARYINFO_REQ".equals(cCatalog)){
                                    cCreator=crmAcceDtlMapper.getCreator(creatorMap);
                                }
                                crmFileBo.setcCreator(cCreator);
                                crmAcceDtlMapper.add(crmFileBo);
                                newCount++;
                            }else{
                                oldCount++;
                            }

                       // }else{
                       //     oldCount++;
                       // }
                        //同时更新crm_URL
                        CrmAcceDtlBo updateParam=new CrmAcceDtlBo();
                        updateParam.setlStorageid(lStorageid);
                        updateParam.setFileUrl(localFilePath);
                        crmAcceDtlMapper.update(updateParam);
                    }

                }

                if (appFileList != null) {
                    //System.out.println(appFileList.size()+"条任务处理完成,其中新增 "+newCount+"条,已存在 "+oldCount+"条");
                } else {
                   // System.out.println("我什么事都不用做，等待下次再说吧");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {

            }

    }
}

